home *** CD-ROM | disk | FTP | other *** search
- Path: ts4-007.jaxnet.com!user
- From: garyg@jax.jaxnet.com (Gary M. Greenberg)
- Newsgroups: comp.lang.c
- Subject: Re: Simple Program Question
- Date: Thu, 29 Feb 1996 20:45:25 -0500
- Organization: Southeast Network Services, Inc.
- Message-ID: <garyg-2902962045250001@ts4-007.jaxnet.com>
- References: <4gsr9u$sk6@newsbf02.news.aol.com> <4gti5g$d78@garden.csc.calpoly.edu>
- NNTP-Posting-Host: ts4-007.jaxnet.com
-
- In article <4gti5g$d78@garden.csc.calpoly.edu>,
- dstubbs@garden.csc.calpoly.edu (Dan Stubbs) wrote:
-
- > In article <4gsr9u$sk6@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
- > >I am trying to write a non -interactive program that calculates all
- > >integer triples (i, j, k) such that
- > >0 < i < j < k < l and i + j + k = l. Print out the number of triples that
- > >satisfy the requirements and print out every millionth triple. Can anyone
- > >see where I am missing the boat in the following code. The program runs
- > >and immediately terminates. Thanks in advance for any feedback.
- > >
-
- Ty:
- All the superflous "()"s in your original code are an indication that you
- were trying to 'force' the order of evaluation in the code to make things
- work and that maybe this is still rather foreign to ya. No sweat. It is
- better imho to liberally sprinkle printf() statements in your code when
- you are learning. If you had preceded the do loop with a printf() of the
- values of i,j,k,l you would have seen 0 0 0 0 and known that nothing
- would occur. The minimal changes below should help you see
- what you want.
-
- > >#include <stdio.h>
- > >
- > >long int i, j, k, l;
- > >long int count;
- > >
- > >int
- > >main (void)
- > >{
-
- l=1;
-
- > > do
- > > {
- > > /* (l = i + j + k);*/
- > > (i = 1);
- > > (j = 2);
- > > (k = 3);
- > > /*(i++, j++, k++);*/
- l = i + j + k;
- i++; j++; k++;
- > > (++count);
- > > if (count % 1000000 == 0)
- > > {
- > > printf("%ld(i) + %ld(j) + %ld(k) = %ld(l)", i, j, k, l);
- > > }
- > >
- > > } while (0 < i < j < k < l);
- > >
- > > return (0);
- > >}
- >
- [snip ...]
- > A couple of problems: the first time l = i+j+k is encountered
- > i, j, and k have had no value assigned and hence what happens is undefined.
- > Perhaps that is where your program dies.
- Bzzzt! the declared variables have global scope && are each automatically
- set to 0 <zero>. That is why the program terminates. The first iteration
- is ... "while (0 < i < j < k < 0);"
- reason enuf!
-
- [big snip ...]
-
- C'ya,
-
- gary /* the Sorcerer's Apprentice */
-
- Free Speech in America murdered by the Communications Decency Act.
- ((U.S. Congress && Executive) == Collective Moron) == 1
-